package cn.duli.xianer.controller;

import cn.duli.xianer.pojo.MyResult;
import cn.duli.xianer.pojo.Notice;
import cn.duli.xianer.pojo.NoticeUser;
import cn.duli.xianer.pojo.Users;
import cn.duli.xianer.service.NoticeService;
import cn.duli.xianer.service.NoticeUserService;
import cn.duli.xianer.service.UsersService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private NoticeUserService noticeUserService;

    @Autowired
    private UsersService usersService;

    /**
     * 发布系统通知给用户
     * @param notice
     * @return
     */
    @RequestMapping("/publish")
    @RequiresRoles("admin")
    public MyResult publish(Notice notice) {
        // 新增系统通知
        int nId = noticeService.save(notice);
        NoticeUser noticeUser = new NoticeUser();
        noticeUser.setNuNid(nId);
        if (notice.getnReceiver()==0) {
            // 发布系统通知给全部用户
            List<Users> usersList = usersService.findAll();
            for (int i=0;i<usersList.size();i++) {
                int uId = usersList.get(i).getuId();
                noticeUser.setNuUid(uId);
                noticeUser.setNuNid(nId);
                int nuUid = noticeUserService.saveOne(noticeUser);
                // 修改通知的发布时间
                noticeService.updatePubTime(noticeService.findById(nId));
            }
        } else {
            // 发布系统通知给指定用户
            noticeUser.setNuUid(notice.getnReceiver());
            int nuUid = noticeUserService.saveOne(noticeUser);
            // 修改通知的发布时间
            noticeService.updatePubTime(noticeService.findById(nId));
        }
        return new MyResult(200, "发布系统通知成功",null,null,null,null);
    }

    /**
     * 根据id查找系统通知详情
     * @param nId
     * @return
     */
    @GetMapping("/findById/{nId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user", "admin"})
    public MyResult findById(@PathVariable("nId") Integer nId) {
        Notice notice = noticeService.findById(nId);
        return new MyResult(200, "根据id查找系统通知详情",null,null,notice,null);
    }

    /**
     * 修改系统通知
     * @param notice
     * @return
     */
    @RequestMapping("/update")
    @RequiresRoles("admin")
    public MyResult update(Notice notice) {
        noticeService.update(notice);
        return new MyResult(200, "修改系统通知",null,null,null,null);
    }

    /**
     * 根据id删除此系统通知
     * @param nId
     * @return
     */
    @RequestMapping("/deleteNoticeById/{nId}")
    @RequiresRoles("admin")
    public MyResult deleteNoticeById(@PathVariable("nId") Integer nId) {
        // 先删除系统通知
        noticeService.deleteById(nId);
        List<NoticeUser> noticeUsers = noticeUserService.findByNid(nId);
        for (int i=0;i<noticeUsers.size();i++) {
            noticeUserService.deleteById(noticeUsers.get(i).getNuId());
        }
        return new MyResult(200, "根据id删除此系统通知",null,null,null,null);
    }

    /**
     * 查看系统通知列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findAll")
    @RequiresRoles("admin")
    public MyResult findAll(@RequestParam(defaultValue = "1") int pageNo,
                                  @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Notice> pageInfo = new PageInfo<>(noticeService.findAll());
        return new MyResult(200, "查看系统通知列表",null,null,null,pageInfo);
    }

    /**
     * 根据用户id查看该用户收到的已读系统通知列表
     * @param uId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/findReadByUid")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findReadByUid(@RequestParam("uId") Integer uId,
                                 @RequestParam(defaultValue = "1") int pageNo,
                                 @RequestParam(defaultValue = "6") int pageSize) {
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<NoticeUser> pageInfo = new PageInfo<>(noticeUserService.findReadByUid(uId));
        return new MyResult(200, "根据用户id查看该用户收到的已读系统通知列表",null,null,null,pageInfo);
    }

    /**
     * 根据用户id查看该用户收到的未读系统通知列表
     * @param uId
     * @return
     */
    @GetMapping("/findNotReadByUid")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult findNotReadByUid(@RequestParam("uId") Integer uId) {
        List<NoticeUser> list = noticeUserService.findNotReadByUid(uId);
        return new MyResult(200, "根据用户id查看该用户收到的未读系统通知列表",null,list,null,null);
    }

    /**
     * 用户修改某条系统通知阅读状态为已读
     * @param noticeUser
     * @return
     */
    @RequestMapping("/updateRead")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult updateRead(NoticeUser noticeUser) {
        noticeUserService.updateRead(noticeUser);
        return new MyResult(200, "用户修改某条系统通知阅读状态为已读",null,null,null,null);
    }

    /**
     * 用户根据通知用户id删除此条通知
     * @param nuId
     * @return
     */
    @RequestMapping("/deleteById/{nuId}")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult deleteById(@PathVariable("nuId") Integer nuId) {
        noticeUserService.deleteById(nuId);
        return new MyResult(200, "用户根据通知用户id删除此条通知",null,null,null,null);
    }

    /**
     * 将某个用户下的未读通知全部标记为已读
     * @param noticeUser
     * @return
     */
    @RequestMapping("/updateAllRead")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult updateAllRead(NoticeUser noticeUser) {
        List<NoticeUser> list = noticeUserService.findNotReadByUid(noticeUser.getNuUid());
        for (int i=0;i<list.size();i++) {
            int nuId = list.get(i).getNuId();
            noticeUser.setNuId(nuId);
            noticeUserService.updateRead(noticeUser);
        }
        return new MyResult(200, "将某个用户下的未读通知全部标记为已读",null,null,null,null);
    }

    /**
     * 将某个用户下的所有系统通知消息清空
     * @param uId
     * @return
     */
    @RequestMapping("/deleteAll")
    @RequiresRoles(logical = Logical.OR, value = {"business", "user"})
    public MyResult deleteAll(@RequestParam("uId") Integer uId) {
        List<NoticeUser> list = noticeUserService.findAll(uId);
        for (int i=0;i<list.size();i++) {
            int nuId = list.get(i).getNuId();
            noticeUserService.deleteById(nuId);
        }
        return new MyResult(200, "将某个用户下的所有系统通知消息清空",null,null,null,null);
    }

}
